<?php

class MiShareAction extends Action {
	//获取分享列表
	public function getMiShareList($name=null,$startTime=null,$endTime=null,$order="ms.createTime", $orderBy="desc", $start = 0, $limit = 25) {				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$cid = C("PLANT_CUSTOMERID");
		Session::switchCustomer(0);
	
		if (!empty($name)) {
			$condition['wm.title'] = array('like',"%$name%");
		}
		
		if ($startTime != '' && $endTime != '') {
			$condition['ms.createTime'] = array(array('egt',$startTime),array('elt',$endTime));
		} else if ($startTime != '') {
			$condition["ms.createTime"] = array('egt', $startTime);
		} else if ($endTime != '') {
			$condition["ms.createTime"] = array('elt', $endTime);
		}
		$MiShareModel = M("mishare");
		Session::switchCustomer($cid);
		$arr=$MiShareModel
		->field("ms.*,wm.*,ms.id as id")
		->alias("ms")
		->join("wx_mishare_material as wm on wm.sId=ms.id")	
		->where($condition)
		->order("$order $orderBy")->limit("$start, $limit")
		->select();
		//log::write('分享列表'.$MiShareModel->getLastSql());
		if ($arr) {
			$count =$MiShareModel
            		->field("ms.*,ms.id as id,wm.*")
            		->alias("ms")
            		->join("wx_mishare_material as wm on wm.sId=ms.id")	
            		->where($condition)
            		//->order("$order $orderBy")->limit("$start, $limit")
            		->count();
			//log::write('分享列表count sql'.$MiShareModel->getLastSql());
				
			Output::instance()->objArray($arr, $count);
		} else {
			Output::instance()->resultCode(code_success);
		}
		
		
	}	

	
		public function getMiShareVisitList($sid,$name=null,$startTime=null,$endTime=null,$vnickname=null,$nickname=null,$order="mv.createTime", $orderBy="desc", $start = 0, $limit = 25) {				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$cid = C("PLANT_CUSTOMERID");
		Session::switchCustomer(0);
		$MiVisitModel = M("mishare_visit");		
		Session::switchCustomer($cid);
		
		
		if (!empty($name)) {
			$where['mi.name'] = array('like',"%$name%");
		}
		
		
		if (!empty($vnickname)) {
			$where['wlx.nickname'] = array('like',"%$vnickname%");
		}
		if (!empty($nickname)) {
			$where['wx.nickname'] = array('like',"%$nickname%");
		}
		if ($startTime != '' && $endTime != '') {
			$where['mv.createTime'] = array(array('egt',$startTime),array('elt',$endTime));
		} else if ($startTime != '') {
			$where["mv.createTime"] = array('egt', $startTime);
		} else if ($endTime != '') {
			$where["mv.createTime"] = array('elt', $endTime);
		}
		
		
		$where["mv.sId"]=$sid;
		$arr=$MiVisitModel
		->field("mv.*,mi.name,wx.headimgurl,wx.nickname,wlx.nickname as vnickname,wlx.headimgurl as vheadimgurl,wxmu.nickname as munickname,wxmu.headimgurl as muheadimgurl")
		->alias("mv")
		->join("wx_mishare_from as mf on mf.guid=mv.fromGuid")
		->join("wx_all_user as au on au.id=mf.uId")
		->join("wx_miyouquan as mi on mi.id=mf.quanId")	
		->join("wx_".$cid."_user_weixin_info as wx on wx.id=au.weixinUserId")
		//获取访问者信息	
		->join("wx_all_user AS alu ON alu.id=mv.uId")	
		->join("wx_".$cid."_user_weixin_info AS wlx ON wlx.id=alu.weixinUserId")	
		 
		
		//获取密友信息	
		->join("wx_all_user AS mu ON mu.id=mv.miUid")	
		->join("wx_".$cid."_user_weixin_info AS wxmu ON wxmu.id=mu.weixinUserId")	
		 
		->where($where)
		->order("$order $orderBy")->limit("$start, $limit")
		->select();
	//	log::write('访问者列表'.$MiVisitModel->getLastSql());
		//echo $MiVisitModel->getLastSql();
		if ($arr) {
			$count = $MiVisitModel
		->field("mv.*,mi.name,wx.headimgurl,wx.nickname,wlx.nickname as vnickname,wlx.headimgurl as vheadimgurl,wxmu.nickname as munickname,wxmu.headimgurl as muheadimgurl")
		->alias("mv")
		->join("wx_mishare_from as mf on mf.guid=mv.fromGuid")
		->join("wx_all_user as au on au.id=mf.uId")
		->join("wx_miyouquan as mi on mi.id=mf.quanId")	
		->join("wx_".$cid."_user_weixin_info as wx on wx.id=au.weixinUserId")
		//获取访问者信息	
		->join("wx_all_user AS alu ON alu.id=mv.uId")	
		->join("wx_".$cid."_user_weixin_info AS wlx ON wlx.id=alu.weixinUserId")	
		 
		
		//获取密友信息	
		->join("wx_all_user AS mu ON mu.id=mv.miUid")	
		->join("wx_".$cid."_user_weixin_info AS wxmu ON wxmu.id=mu.weixinUserId")	
		 
		->where($where)
		//->order("$order $orderBy")->limit("$start, $limit")
		->count();
		//	log::write('访问者列表count sql'.$MiVisitModel->getLastSql());
			Output::instance()->objArray($arr, $count);
		} else {
			Output::instance()->resultCode(code_success);
		}
		
		
	}		
	
	
	public function getMiShareFromList($sid,$name=null,$startTime=null,$endTime=null,$fromPage=null,$shareType=null,$nickname=null,$order="mf.createTime", $orderBy="desc", $start = 0, $limit = 25) {				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$cid = C("PLANT_CUSTOMERID");
		Session::switchCustomer(0);
		$MiFromModel = M("mishare_from");		
		Session::switchCustomer($cid);
		
		
		
	if (!empty($fromPage)) {
			$where['mf.fromPage'] = $fromPage;
		}
	if (!empty($shareType)) {
			$where['mf.shareType'] =$shareType;
		}
		
		if (!empty($name)) {
			$where['mi.name'] = array('like',"%$name%");
		}
		
	if (!empty($nickname)) {
			$where['wx.nickname'] = array('like',"%$nickname%");
		}
		if ($startTime != '' && $endTime != '') {
			$where['mf.createTime'] = array(array('egt',$startTime),array('elt',$endTime));
		} else if ($startTime != '') {
			$where["mf.createTime"] = array('egt', $startTime);
		} else if ($endTime != '') {
			$where["mf.createTime"] = array('elt', $endTime);
		}
		
		$where["mf.sId"]=$sid;
		$arr=$MiFromModel
		->field("mf.*,au.fullName,mi.name,mi.logo,wx.nickname,wx.headimgurl,wxmu.nickname as munickname,wxmu.headimgurl as muheadimgurl")
		->alias("mf")
		//->join("wx_mishare_from as wmf on wmf.parentFromGuid=mf.guid")
		->join("wx_all_user as au on au.id=mf.uId")
		->join("wx_miyouquan as mi on mi.id=mf.quanId")	
		->join("wx_".$cid."_user_weixin_info as wx on wx.id=au.weixinUserId")

		//获取密友信息	
		->join("wx_all_user AS mu ON mu.id=mf.miUid")	
		->join("wx_".$cid."_user_weixin_info AS wxmu ON wxmu.id=mu.weixinUserId")	
		 
		->where($where)
		->order("$order $orderBy")->limit("$start, $limit")
		->select();
	//log::write('分享者列表'.$MiFromModel->getLastSql());
		//echo $MiFromModel->getLastSql();
		if ($arr) {
			$count = $MiFromModel
            		->field("mf.*,au.fullName,mi.name,mi.logo,wx.nickname,wx.headimgurl,wxmu.nickname as munickname,wxmu.headimgurl as muheadimgurl")
            		->alias("mf")
            		//->join("wx_mishare_from as wmf on wmf.parentFromGuid=mf.guid")
            		->join("wx_all_user as au on au.id=mf.uId")
            		->join("wx_miyouquan as mi on mi.id=mf.quanId")	
            		->join("wx_".$cid."_user_weixin_info as wx on wx.id=au.weixinUserId")
            
            		//获取密友信息	
            		->join("wx_all_user AS mu ON mu.id=mf.miUid")	
            		->join("wx_".$cid."_user_weixin_info AS wxmu ON wxmu.id=mu.weixinUserId")	
            		 
            		->where($where)
            		//->order("$order $orderBy")->limit("$start, $limit")
            		->count();
		//	log::write('分享者列表 count'.$MiFromModel->getLastSql());
			Output::instance()->objArray($arr, $count);
		} else {
			Output::instance()->resultCode(code_success);
		}
		
		
	}		
}
